package com.unipay.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.org.apache.regexp.internal.RE;
import com.unipay.common.result.MD5;
import com.unipay.common.result.Result;
import com.unipay.model.system.SysUser;
import com.unipay.model.vo.SysUserQueryVo;
import com.unipay.system.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author cmy
 * @since 2023-02-07
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/admin/system/sysUser")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;
    
    // 1、条件分页查询
    @ApiOperation(value = "条件分页查询")
    @GetMapping("/{page}/{limit}")
    public Result userPageSel(@PathVariable Long page,
                              @PathVariable Long limit,
                              SysUserQueryVo sysUserQueryVo){
        //（1）创建page对象
        Page<SysUser> pageParam = new Page<>(page, limit);
        //（2）调用service方法
        IPage<SysUser> pageModel = sysUserService.selPage(pageParam, sysUserQueryVo);
        return Result.ok(pageModel);
    }

    // 2、添加
    @ApiOperation("添加用户")
    @PostMapping("/userSave")
    public Result userSave(@RequestBody SysUser sysUser){
        // TODO 用户密码进行加密 MD5
        sysUser.setPassword(MD5.encrypt(sysUser.getPassword()));
        boolean save = sysUserService.save(sysUser);
        if (save){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    // 3、更新用户（一、根据ID查询；二，保存修改）
    @ApiOperation("ID查询用户")
    @GetMapping("/userGet/{id}")
    public Result userGet(@PathVariable("id") Long id){
        SysUser sysUser = sysUserService.getById(id);
        return Result.ok(sysUser);
    }
    @ApiOperation("更新用户")
    @PutMapping("/userUpdate")
    public Result userUpdate(@RequestBody SysUser sysUser){
        boolean update = sysUserService.updateById(sysUser);
        if (update){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    // 4、删除用户
    @ApiOperation("ID删除用户")
    @DeleteMapping("/userRemove/{id}")
    public Result userRemove(@PathVariable Long id){
        boolean remove = sysUserService.removeById(id);
        if (remove){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    // 5、批量删除用户
    @ApiOperation("批量删除用户")
    @DeleteMapping("/userBatchRemove")
    //public Result userBatchRemove(@PathVariable("idList") List<Long> idList){
    public Result userBatchRemove(@RequestBody List<Long> idList){
        boolean removeByIds = sysUserService.removeByIds(idList);
        if (removeByIds){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    // 6、更新用户状态
    @ApiOperation(value = "更新用户状态")
    @GetMapping("/updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable("id") Long id, @PathVariable("status") Integer status){
        int i = sysUserService.updateStatus(id, status);
        if (i > 0){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
}

